package com.zy.paratest.controller;

import com.zy.paratest.domain.Girl;
import com.zy.paratest.service.GirlService;
import com.zy.paratest.repository.GrilRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class GirlController {

    @Autowired
    private GrilRepository grilRepository;

    @Autowired
    private GirlService girlService;

    //查询列表
    @GetMapping(value = "/girl")
    public List<Girl> girlList() {
        return grilRepository.findAll();
    }

    //新增人员
    @PostMapping(value = "/new")
    public Girl girlAdd(@RequestParam("name") String name,
                        @RequestParam("weight") Integer weight,
                        @RequestParam("height") Integer height,
                        @RequestParam("age") Integer age) {

        Girl girl = new Girl();
        girl.setAge(age);
        girl.setHeight(height);
        girl.setWeight(weight);
        girl.setName(name);

        return grilRepository.save(girl);
    }

    //查询一个人员，利用系统自带id查询
    @PostMapping(value = "/girl")
    public Girl girlFindOne(@RequestParam("id") Integer id) {
        return grilRepository.findById(id).get();
    }

    //查询一个人员，通过名称或身高查询
    @PostMapping(value = "/girl/list")
    public List<Girl> girlListFind(@RequestParam("name") String name) {
        return grilRepository.findByName(name);
    }

    //更新数据
    @PutMapping(value = "/update")
    public Girl girlUpdate(
            @RequestParam("id") Integer id,
            @RequestParam("age") Integer age) {

        Girl girl = new Girl();
        girl.setId(id);
        girl.setAge(age);

        return grilRepository.save(girl);

    }

    //删除数据
    @DeleteMapping(value = "/delete/{id}")
    public void girlDelete(@PathVariable("id") Integer id) {
        grilRepository.deleteById(id);
    }

    //测试事务管理
    @PostMapping(value = "/girl/trans")
    public void insert() {

        girlService.girlAddtwo();

    }

}
